package com.js.data.system.controller;


import com.js.data.common.api.CommonResult;
import com.js.data.system.annotation.Log;
import com.js.data.system.dto.BatchRolePermDTO;
import com.js.data.system.dto.RoleDTO;
import com.js.data.system.dto.RoleUserDTO;
import com.js.data.system.query.RoleQuery;
import com.js.data.system.vo.RolePageVO;
import com.js.data.system.vo.SimpleRoleVO;
import com.js.data.system.vo.SimpleUserVO;
import com.js.data.system.service.RoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author wujj
 * @since 2022-09-06
 */

@RestController
@RequestMapping("/sys")
public class RoleController {

    @Resource
    private RoleService roleService;


    /**
     * 角色分页
     *
     * @param query 查询条件
     * @return IPage<RolePageVO>
     */
    @PreAuthorize("@perm.check('system:role:list')")
    @GetMapping(value = "/role/page")
    public CommonResult<IPage<RolePageVO>> pageRole(@ModelAttribute RoleQuery query) {
        return CommonResult.success(roleService.pageRole(query));
    }


    /**
     * 新增角色
     *
     * @param roleDTO 角色表单
     * @return void
     */
    @PreAuthorize("@perm.check('system:role:save')")
    @Log(value = "新增角色")
    @PostMapping(value = "/role")
    public CommonResult<Void> saveRole(@Validated @RequestBody RoleDTO roleDTO) {
        roleService.saveRole(roleDTO);
        return CommonResult.success();
    }

    /**
     * 修改角色
     *
     * @param id      角色id
     * @param roleDTO 角色表单
     * @return void
     */
    @PreAuthorize("@perm.check('system:role:update')")
    @Log(value = "修改角色")
    @PutMapping(value = "/role/{id}")
    public CommonResult<Void> editRole(@PathVariable Long id, @Validated @RequestBody RoleDTO roleDTO) {
        roleService.editRole(id, roleDTO);
        return CommonResult.success();
    }

    /**
     * 删除角色
     *
     * @param id 角色ids
     * @return void
     */
    @PreAuthorize("@perm.check('system:role:delete')")
    @Log(value = "删除角色")
    @DeleteMapping(value = "/role/{id}")
    public CommonResult<Void> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return CommonResult.success();
    }

    /**
     * 启用角色
     *
     * @param id 角色id
     * @return void
     */
    @PreAuthorize("@perm.check('system:role:status')")
    @Log(value = "启用角色")
    @PutMapping(value = "/role/{id}/state")
    public CommonResult<Void> stateRole(@PathVariable Long id) {
        roleService.stateRole(id);
        return CommonResult.success();
    }

    /**
     * 返回简单角色信息
     *
     * @return List<SimpleRoleVO>
     * @author wujj
     * @since 2020/6/6 14:05
     */
    @GetMapping(value = "/role/simple", produces = "application/json; charset=utf-8")
    public CommonResult<List<SimpleRoleVO>> listSimpleRole(@RequestParam(name = "roleName", required = false) String roleName) {
        return CommonResult.success(roleService.listSimpleRole(roleName));
    }

    /**
     * 根据角色查找关联用户列表
     *
     * @param id 角色id
     * @return List<SimpleUserVO>
     * @author wujj
     * @since 2020/6/8 17:25
     */
    @GetMapping(value = "/role/{id}/simple/user", produces = "application/json; charset=utf-8")
    public CommonResult<List<SimpleUserVO>> listSimpleUserByRoleId(@PathVariable Long id) {
        return CommonResult.success(roleService.listSimpleUserByRoleId(id));
    }

    /**
     * 角色下人员
     *
     * @param roleUserDTO 角色下用户
     * @return void
     */
    @PreAuthorize("@perm.check('system:role:user:add')")
    @Log(value = "设置角色下人员")
    @PostMapping(value = "/role/user", produces = "application/json; charset=utf-8")
    public CommonResult<Void> saveRoleUser(@RequestBody RoleUserDTO roleUserDTO) {
        roleService.saveRoleUser(roleUserDTO);
        return CommonResult.success();
    }

    /**
     * 角色功能权限列表
     *
     * @param roleId 角色id
     * @return List<Integer>
     * @author wujj
     * @since 2020/6/12 14:19
     */
    @GetMapping(value = "/role/{roleId}/menu/perm", produces = "application/json; charset=utf-8")
    public CommonResult<List<Long>> listRoleMenuPerm(@PathVariable Long roleId) {
        return CommonResult.success(roleService.listRoleMenuPerm(roleId));
    }


    /**
     * 角色菜单功能权限
     *
     * @param batchRolePermFrom 批量角色修改表单
     * @return Integer
     * @author wujj
     * @since 2020/6/5 14:26
     */
    @PreAuthorize("@perm.check('system:role:menu:perm')")
    @Log(value = "设置角色菜单")
    @PutMapping(value = "/role/menu-perm", produces = "application/json; charset=utf-8")
    public CommonResult<Void> roleMenuPerm(@RequestBody BatchRolePermDTO batchRolePermFrom) {
        roleService.roleMenuPerm(batchRolePermFrom);
        return CommonResult.success();
    }
}
